package com.anyou.mall.goo.goods.domain;

import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.http.HtmlUtil;
import cn.hutool.json.JSONUtil;
import com.anyou.common.core.domain.BaseEntity;
import com.anyou.mall.goo.goods.dto.GoodsOperationDTO;
import com.baomidou.mybatisplus.annotation.*;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * goods对象 li_goods
 *
 * @author xiaQ
 * @date 2023-09-15
 */
@Data
@AllArgsConstructor
@EqualsAndHashCode(callSuper=false)
@ApiModel(value = "goods")
@TableName(value = "goo_goods")
public class GooGoodsVo extends BaseEntity
{
    private static final long serialVersionUID = 1L;

    /** ID */

    @ApiModelProperty("goodsId")
    private Long goodsId;


    @ApiModelProperty(value = "商品名称")
    private String goodsName;

    @ApiModelProperty(value = "商品价格", required = true)
    private Double price;

    @ApiModelProperty(value = "商品价格", required = true)
    private Double rawPrice;

    @ApiModelProperty(value = "商品运费", required = true)
    private Double freight;

    @ApiModelProperty(value = "分类path")
    private String categoryPath;

    @TableField(exist = false)
    @ApiModelProperty(value = "分类Name")
    private String categoryNameStr;

    @ApiModelProperty(value = "商品类型（0普通商品，1水票商品）")
    private Integer goodType;

    @ApiModelProperty(value = "计量单位")
    private String goodsUnit;



    /**
     * 上架状态（0上架 1下架）
     */
    @ApiModelProperty(value = "上架状态（0上架 1下架）")
    private String marketEnable;

    @ApiModelProperty(value = "详情")
    private String intro;

    @ApiModelProperty(value = "销量")
    private Integer buyCount;

    @ApiModelProperty(value = "最低购买数")
    private Integer minBuyNumber;


    @ApiModelProperty(value = "库存")
    private Integer quantity;

    @ApiModelProperty(value = "商品好评率")
    private Double grade;

    @ApiModelProperty(value = "商品图片路径")
    private String original;

    @ApiModelProperty(value = "评论数量")
    private Integer commentNum;

    @ApiModelProperty(value = "卖家id")
    private Long storeId;

    @ApiModelProperty(value = "卖家名字")
    private String storeName;

    /**
     * 审核状态（0待审核 1通过 2不通过）
     */
    @ApiModelProperty(value = "审核状态（0待审核 1通过 2不通过）")
    private String auditStatus;

    @ApiModelProperty(value = "审核信息")
    private String auditRemark;

    @ApiModelProperty(value = "下架原因")
    private String underMessage;

    @ApiModelProperty(value = "商品移动端详情")
    private String mobileIntro;

    @ApiModelProperty(value = "商品视频")
    private String goodsVideo;


    @ApiModelProperty(value = "是否为推荐商品（0否 1是）", required = true)
    private String appIndexStatus;

    @ApiModelProperty(value = "商品参数json", hidden = true)
    private String goodParams;

    @TableLogic( value = "0",delval = "2" )
    @ApiModelProperty(value = "（0代表存在 2代表删除）")
    private String delFlag;


    @ApiModelProperty("是否为积分商品（0-否，1-是）")
    private Integer isJf;

    @ApiModelProperty("积分商品类型（0-实体商品，1-虚拟商品）")
    private String jfType;

    @ApiModelProperty("积分商品库存")
    private Integer jfStock;

    @ApiModelProperty("积分值")
    private Integer jfValue;

    @ApiModelProperty(value = "赠送积分值")
    private Integer giveIntegralNum;

    @ApiModelProperty(value = "是否可用水票（0可以使用，1不能使用）")
    private Integer ifWaterTicket;

    @ApiModelProperty(value = "库存预警")
    private Integer stockWarning;

    @ApiModelProperty(value = "一级分类id")
    @TableField("lavel_one_id")
    private Long lavelOneId;


    public GooGoodsVo() {
    }

    public GooGoodsVo(GoodsOperationDTO goodsOperationDTO) {
        if (goodsOperationDTO.getGoodsId()!=null){
            this.goodsId=goodsOperationDTO.getGoodsId();
        }
        this.storeId = goodsOperationDTO.getStoreId();
        this.goodsName = goodsOperationDTO.getGoodsName();
        this.categoryPath = goodsOperationDTO.getCategoryPath();
        this.appIndexStatus = goodsOperationDTO.getAppIndexStatus();
        /*this.sellingPoint = goodsOperationDTO.getSellingPoint();*/
        /*this.intro = goodsOperationDTO.getIntro();*/
        this.mobileIntro = goodsOperationDTO.getMobileIntro();
        this.goodsVideo = goodsOperationDTO.getGoodsVideo();
        this.price = goodsOperationDTO.getPrice();
        this.rawPrice = goodsOperationDTO.getRawPrice();
        /*this.freight = goodsOperationDTO.getFreight();*/
        this.minBuyNumber=goodsOperationDTO.getMinBuyNumber();
        this.giveIntegralNum=goodsOperationDTO.getGiveIntegralNum();
        this.ifWaterTicket=goodsOperationDTO.getIfWaterTicket();
        this.goodType=goodsOperationDTO.getGoodType();
        this.stockWarning=goodsOperationDTO.getStockWarning();
        if (goodsOperationDTO.getGoodsParamsDTOList() != null && goodsOperationDTO.getGoodsParamsDTOList().isEmpty()) {
            this.goodParams = JSONUtil.toJsonStr(goodsOperationDTO.getGoodsParamsDTOList());
        }
        this.grade = 100D;

        //循环sku，判定sku是否有效
        /*for (Map<String, Object> sku : goodsOperationDTO.getGoodsSkus()) {
            //判定参数不能为空
            *//*if (!sku.containsKey("sn") || sku.get("sn") == null) {
                throw new ServiceException("商品SKU货号不能为空");
            }*//*
            if ((!sku.containsKey("price") || StringUtil.isEmpty(sku.get("price").toString()) || Convert.toDouble(sku.get("price")) <= 0) ) {
                throw new ServiceException("商品SKU价格不能小于等于0");
            }
            *//*if ((!sku.containsKey("cost") || StringUtil.isEmpty(sku.get("cost").toString()) || Convert.toDouble(sku.get("cost")) <= 0) ) {
                throw new ServiceException("商品SKU成本价不能小于等于0");
            }*//*
            if (!sku.containsKey("quantity") || StringUtil.isEmpty(sku.get("quantity").toString()) || Convert.toInt(sku.get("quantity").toString()) < 0) {
                throw new ServiceException("商品库存数量不能为负数");
            }
            sku.values().forEach(i -> {
                if (CharSequenceUtil.isBlank(i.toString())) {
                    throw new ServiceException("规格值不能为空");
                }
            });


        }*/
    }


    public String getIntro() {
        if (CharSequenceUtil.isNotEmpty(intro)) {
            return HtmlUtil.unescape(intro);
        }
        return intro;
    }

    public String getMobileIntro() {
        if (CharSequenceUtil.isNotEmpty(mobileIntro)) {
            return HtmlUtil.unescape(mobileIntro);
        }
        return mobileIntro;
    }



}
